import {
  getHotSingerListRequest,
  getSingerListRequest
} from '../../../api/request'
import * as actionTypes from './constant'
import { fromJS } from 'immutable'

const changeSingerList = data => ({
  type: actionTypes.CHANGE_SINGER_LIST,
  data: fromJS(data)
})

// export const changeAlpha = data => ({
//   type: actionTypes.CHANGE_ALPHA,
//   data: data
// })

// export const changeType = data => ({
//   type: actionTypes.CHANGE_TYPE,
//   data: data
// })

// export const changeArea = data => ({
//   type: actionTypes.CHANGE_AREA,
//   data: data
// })

export const changeOffset = data => ({
  type: actionTypes.CHANGE_OFFSET,
  data: data
})

export const changeEnterLoading = data => ({
  type: actionTypes.CHANGE_ENTER_LOADING,
  data
})

export const changePullUpLoading = data => ({
  type: actionTypes.CHANGE_PULLUP_LOADING,
  data
})

export const changePullDownLoading = data => ({
  type: actionTypes.CHANGE_PULLDOWN_LOADING,
  data
})

// export const handleAlphaUpdate = val => {
//   return (dispatch, getState) => {
//     const type = getState().getIn(['singers', 'type'])
//     const area = getState().getIn(['singers', 'area'])
//     const offset = getState().getIn(['singers', 'offset'])

//     dispatch(changeAlpha(val))
//     console.log(getState().getIn(['singers', 'alpha']))
//     getSingerListRequest(type, area, val, offset).then(res => {
//       const data = res.artists

//       dispatch(changeSingerList(data))
//       dispatch(changeEnterLoading(false))
//       dispatch(changePullDownLoading(false))
//     })
//   }
// }

// export const handleTypeUpdate = val => {
//   return (dispatch, getState) => {
//     const alpha = getState().getIn(['singers', 'alpha'])
//     const area = getState().getIn(['singers', 'area'])
//     const offset = getState().getIn(['singers', 'offset'])

//     dispatch(changeType(val))
//     getSingerListRequest(val, area, alpha, offset).then(res => {
//       const data = res.artists

//       dispatch(changeSingerList(data))
//       dispatch(changeEnterLoading(false))
//       dispatch(changePullDownLoading(false))
//     })
//   }
// }

// export const handleAreaUpdate = val => {
//   return (dispatch, getState) => {
//     const alpha = getState().getIn(['singers', 'alpha'])
//     const type = getState().getIn(['singers', 'type'])
//     const offset = getState().getIn(['singers', 'offset'])

//     dispatch(changeArea(val))
//     getSingerListRequest(type, val, alpha, offset).then(res => {
//       const data = res.artists

//       dispatch(changeSingerList(data))
//       dispatch(changeEnterLoading(false))
//       dispatch(changePullDownLoading(false))
//     })
//   }
// }

export const getHotSingerList = () => {
  return dispatch => {
    getHotSingerListRequest(0)
      .then(res => {
        const data = res.artists

        dispatch(changeSingerList(data))
        dispatch(changeEnterLoading(false))
        dispatch(changePullDownLoading(false))
        // dispatch(changeOffset(data.length))
      })
      .catch(() => {
        console.log('热门歌手数据获取失败')
      })
  }
}

export const refreshMoreHotSingerList = () => {
  return (dispatch, getState) => {
    const offset = getState().getIn(['singers', 'offset'])
    const singerList = getState().getIn(['singers', 'singerList']).toJS()

    getHotSingerListRequest(offset)
      .then(res => {
        const data = [...singerList, ...res.artists]

        dispatch(changeSingerList(data))
        dispatch(changePullUpLoading(false))
        dispatch(changeOffset(data.length))
      })
      .catch(() => {
        console.log('热门歌手数据获取失败')
      })
  }
}

export const getSingerList = (type, area, alpha) => {
  return dispatch => {
    getSingerListRequest(type, area, alpha, 0)
      .then(res => {
        const data = res.artists

        dispatch(changeSingerList(data))
        dispatch(changeEnterLoading(false))
        dispatch(changePullDownLoading(false))
        // dispatch(changeOffset(data.length))
      })
      .catch(() => {
        console.log('歌手数据获取失败')
      })
  }
}

export const refreshMoreSingerList = (type, area, alpha) => {
  return (dispatch, getState) => {
    const offset = getState().getIn(['singers', 'offset'])
    const singerList = getState().getIn(['singers', 'singerList']).toJS()

    getSingerListRequest(type, area, alpha, offset)
      .then(res => {
        const data = [...singerList, ...res.artists]

        dispatch(changeSingerList(data))
        dispatch(changePullUpLoading(false))
        dispatch(changeOffset(data.length))
      })
      .catch(() => {
        console.log('获取歌手数据失败')
      })
  }
}
